import dayjs from 'dayjs'
import route from '@/router'
import { useStore } from '@/stores/index'

export const GetUserinfo = () => {
    const store = useStore()
    return store.userinfo
}

export const doLogout = () => {
    const store = useStore()
    return store.logout()
}

export const BaseHost = function(url: string) {
    const env = import.meta.env
    const host = env.VITE_APP_URL
    return `${host}${url}`
}

export const $Date = function(time?: dayjs.ConfigType, format = "YYYY-MM-DD HH:mm:ss") {
    if(!time) {
        time = dayjs().valueOf()
    }
    return dayjs(time).format(format)
}

export const $DateTimeStamp = function(time?: dayjs.ConfigType) {
    return dayjs(time).valueOf()
}

export const $Params = () => {
    return route.currentRoute.value.query
}

export const $Clone = (data = {}) => {
    return JSON.parse(JSON.stringify(data))
}

export const isMobile = () => {
    let breakpoint = 780
    let bWidth = window.innerWidth;
    return bWidth <= breakpoint
}

export const Paginate = (page: number,limit: number,total: number) => {
    return {
        current: page,
        total: total,
        pageSize: limit,
        size: 'small',
        showSizeChanger: true,
        showTotal: () => `共${total}條記錄`,
        pageSizeOptions: [1,10,20]
    }
}

export const RandomColor = (index: number) => {
    const color = ["default","#2db7f5","#87d068","#108ee9"]
    var i = index%color.length
    return color[i]
}

export const isAuth = (value: string[]) => {
    if(value.length == 0) {
        return true
    }
    const store = useStore()
    let can = false
    var auth = store.Auth
    for(let i in value) {
        if(auth.includes(value[i])) {
            can =  true
            break
        }
    }
    return can
}

export const WalletFiex = (wallet: string, fixed: number = 2) => {
    return Number(wallet).toFixed(fixed)
}


